package com.xin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.entity.crm.RolePermissionEntity;
import com.xin.mapper.RolePermissionMapper;
import com.xin.service.RolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 角色所拥有的权限信息
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermissionEntity> implements RolePermissionService {


    @Override
    public List<RolePermissionEntity> findRoleHasPermission(Long roleId) {

        List<RolePermissionEntity> roleHasPermissionList = this.baseMapper.findRoleHasPermissionList(roleId);
        return roleHasPermissionList;
    }


    /**
     * 修改角色所拥有的权限信息
     * @param sns
     * @return
     */
    @Override
    @Transactional
    public Integer updatePermissionToRole(List<String> sns,Long roleId) {
        //先删除角色所拥有的权限信息
        int result = this.baseMapper.delete(new QueryWrapper<RolePermissionEntity>().eq("role_id", roleId));
        //在添加角色拥有的权限信息
        List<RolePermissionEntity> rolePermissionEntityList = new ArrayList<>();
        if (sns!=null&&sns.size()>0)
        for (String sn : sns) {
            RolePermissionEntity rolePermissionEntity = new RolePermissionEntity();
            rolePermissionEntity.setRoleId(roleId);
            rolePermissionEntity.setPermissionSn(sn);
            rolePermissionEntityList.add(rolePermissionEntity);
        }
        boolean b = this.saveBatch(rolePermissionEntityList);

        return 1;
    }
}
